﻿using System;
using System.Collections.Generic;
using System.Xml;

namespace Ue.Extensions
{
    public static class XmlReaderExtensions
    {
        public static IEnumerable<KeyValuePair<string, string>> GetAttributeValues(this XmlReader reader)
        {
            return reader.GetAttributeValues(null);
        }
        /// <summary>
        /// 读取节点Attribute
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="selector"></param>
        /// <returns></returns>
        public static IEnumerable<KeyValuePair<string, string>> GetAttributeValues(this XmlReader reader, Func<string, bool> selector)
        {
            Dictionary<string, string> pairs = new Dictionary<string, string>();

            if (reader.AttributeCount > 0)
            {
                while (reader.MoveToNextAttribute())
                {
                    if (selector == null || (selector != null && selector(reader.Name)))
                    {
                        pairs.Add(reader.Name, reader.Value);
                    }
                }
            }

            reader.MoveToElement();

            return pairs;
        }
    }
}
